package cn.fancy.sdk.pay.utils;

import android.content.Context;
import android.util.Log;
import cn.fancy.sdk.FancyManager;
import cn.fancy.sdk.entity.GuildBaseInfo;
import cn.fancy.sdk.login.utils.HttpReq;
import com.bytedance.applog.game.GameReportHelper;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zhaojh on date 2023/11/29
 */
public class PayAdvertUpload {
    
    public static void uploadPayIfExistAdvert(final Context context, final String platform, final String item_desc, final String pros_id, final int money) {
        if (!FancyManager.getInstance().isAdvertIdBool()) {
            return;
        }
        
        final Map<String, String> map = GuildBaseInfo.getInstance().advertPayUploadParam(context, pros_id);
        Log.d("PayAdvertUpload", "advertPayUploadParam:" + map);
        Thread thread = new Thread(new Runnable() {
            
            @Override
            public void run() {
                // delay 60s请求
                try {
                    Thread.sleep(1000 * 60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                String rst = HttpReq.doGuildRequest(map);
                Log.d("PayAdvertUpload", "PayAdvertUpload rst:" + rst);
                
                try {
                    JSONObject json = new JSONObject(rst);
                    String state = json.getString("state");
                    JSONObject jsonState = new JSONObject(state);
                    int code = jsonState.getInt("code");
                    if (code != 1) {
                        return;
                    }
                    
                    String msg = json.getString("data");
                    JSONObject msgJson = new JSONObject(msg);
                    String pros_idStr = msgJson.getString(pros_id);
                    JSONObject pros_idJson = new JSONObject(pros_idStr);
                    int status = pros_idJson.optInt("status");
                    if (status == 1) {
                        //已经支付
                        GameReportHelper.onEventPurchase(item_desc, item_desc, pros_id, 1, platform, "¥", true, money);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }
}
